﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Warehouse.Models;

namespace Warehouse.Controllers
{
    public class ModelDeviceController : Controller
    {
        private readonly WarehouseEntities _db = new WarehouseEntities();

        //
        // GET: /ModelDevice/

        public ViewResult Index()
        {
            var modeldevice = _db.ModelDevice.Include("TypeDevice");
            return View(modeldevice.ToList());
        }

        public ViewResult Details(int id)
        {
            ModelDevice modeldevice = _db.ModelDevice.Single(m => m.ID == id);
            return View(modeldevice);
        }

        public ActionResult Create()
        {
            ViewBag.TypeID = new SelectList(_db.TypeDevice, "ID", "Title");
            return View();
        }

        [HttpPost]
        public ActionResult Create(ModelDevice modeldevice)
        {
            if (ModelState.IsValid)
            {
                _db.ModelDevice.AddObject(modeldevice);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.TypeID = new SelectList(_db.TypeDevice, "ID", "Title", modeldevice.TypeID);
            return View(modeldevice);
        }

        public ActionResult Edit(int id)
        {
            ModelDevice modeldevice = _db.ModelDevice.Single(m => m.ID == id);
            ViewBag.TypeID = new SelectList(_db.TypeDevice, "ID", "Title", modeldevice.TypeID);
            return View(modeldevice);
        }

        [HttpPost]
        public ActionResult Edit(ModelDevice modeldevice)
        {
            if (ModelState.IsValid)
            {
                _db.ModelDevice.Attach(modeldevice);
                _db.ObjectStateManager.ChangeObjectState(modeldevice, EntityState.Modified);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TypeID = new SelectList(_db.TypeDevice, "ID", "Title", modeldevice.TypeID);
            return View(modeldevice);
        }

        public ActionResult Delete(int id)
        {
            ModelDevice modeldevice = _db.ModelDevice.Single(m => m.ID == id);
            return View(modeldevice);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ModelDevice modeldevice = _db.ModelDevice.Single(m => m.ID == id);
            _db.ModelDevice.DeleteObject(modeldevice);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}